using System;
using System.Threading;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Athena
{
    static class Program
    {
        private static Model _Model = new Model();
        
        public static Model Model
        {
            get
            {
                return _Model;
            }
            set
            {
                _Model = value;
                _Model.DeselectItems(false);
                Utility.Refresh();
            }
        }

        private static MainForm _Form;
        public static MainForm Form
        {
            get
            {
                return _Form;
            }
            set
            {
                _Form = value;
            }
        }

        /// <summary>
        /// The main entry PointF for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            //Thread splash = new Thread(new ThreadStart(Splash.DoSplash));
            //splash.Start();
            Splash.Start();
            
            PluginManager.Instance.InstantiateTypes();
            Form = new MainForm(args);
            Preferences.InitializeSettings();

            try
            {
                IO.SQL.InitializeDB(Utility.CurrentDirectory + "\\DB\\Registry");
                //IO.SQL.ReadTabFile("DB/list.txt");
                Model.Clear(false);
                Core.Module.ClearGlobals();
            }
            catch(Exception)
            {
                //show error: DB not loaded
            }

            try
            {
                Splash.End();
                Form.ParseCommandLine(args);
                Application.Run(Form);   //main program
            }
            catch(Exception)
            {
                //error in main form
            }

            try
            {
                IO.SQL.CloseDB();
            }
            catch (Exception)
            {
                //show error: DB could not close
            }
        }
    }
}